﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.IO;
using System.Diagnostics;
using System.Threading;

namespace BotStart
{
    class Program
    {
        static string path = ConfigurationManager.AppSettings["path"];
        static string timespanString = ConfigurationManager.AppSettings["timespan"];

        static void Main(string[] args)
        {
            if (string.IsNullOrEmpty(path))
            {
                PrintAndWaitToExit("path未配置！"); return;
            }

            if (!Directory.Exists(path))
            {
                PrintAndWaitToExit("path配置的目录不存在！"); return;
            }

            int timespan = 0;
            if (!int.TryParse(timespanString, out timespan) || timespan <= 0)
            {
                Console.WriteLine("timespan配置错误，采用默认值：1分钟！");
                timespan = 1;
            }

            DirectoryInfo info = new DirectoryInfo(path);
            var files = info.GetFiles("*.exe", SearchOption.AllDirectories);
            int count = 0;
            for (var i = 0; i < files.Length; i++)
            {
                var x = files[i];
                if (x.Name.ToLower().Contains(".vshost."))
                {
                    continue;
                }
                count++;
                Console.WriteLine("启动程序" + x.FullName);
                Process p = new Process();
                p.StartInfo = new ProcessStartInfo(x.FullName);
                p.StartInfo.WorkingDirectory = x.DirectoryName;
                p.Start();
                if (i < files.Length - 1)
                {
                    Thread.Sleep(timespan*60*1000);
                }
            }

            files.ToList().ForEach(x =>
            {

            });
            PrintAndWaitToExit(count + "个程序已启动！");
        }

        static void PrintAndWaitToExit(string text)
        {
            Console.WriteLine(text);
            Console.Write("按回车键退出...");
            Console.ReadLine();
        }
    }
}
